<template>
  <div class="config-margin">
    <affix :label="$t('configLazyLoad.robotSeven.dictionaryList')">
      <div class="search">
        <span  @keyup.enter="search">
          <el-input
            size="mini"
            :placeholder="$t('configLazyLoad.robotSeven.pleaseInputSearchContent')"
            v-model="searchObj.name"
            style="width:300px"
          >
            <i
              class="el-input__icon el-icon-search"
              slot="suffix"
              @click="search">
            </i>
          </el-input>
        </span>
        <el-button type="primary" class="primary_btn margin-l" @click="operate({type: 'add'})">{{$t('public.add')}}</el-button>
      </div>
      <common-table :tableHead="tableHead" :data="tableData" :pagination="pagination" @operate="operate" @turnPage="search">
      </common-table>
    </affix>
    <el-dialog :close-on-click-modal="false" :title="commonDialog.title" :visible.sync="commonDialog.isShow">
      <el-form label-position="top" ref="form" :model="form" class="form" :rules="rules">
        <el-form-item :label="$t('configLazyLoad.robotSeven.dictionaryName')" prop="name">
          <el-input size="mini" v-model.trim="form.name"></el-input>
        </el-form-item>
        <el-form-item style="text-align: center">
          <el-button class="primary_btn" @click="commonDialog.isShow = false">{{$t('public.cancel')}}</el-button>
          <el-button type="primary" class="primary_btn" @click="submitForm">{{$t('public.save')}}</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
  import affix from '../../../ui-modules/affix/Affix.vue'
  import commonTable from './base/CommonTable'
  import {getCache} from '@/utils/m7Utils'
  import * as uuid from 'uuid'
  export default {
    name: 'intelligentStudy',
    data () {
      return {
        pagination: {
          currentPage: 0,
          count: 0,
          totalPage: 0
        },
        searchObj: {
          name: '',
          page: 1,
          limit: 10
        },
        commonDialog: {
          isShow: false,
          title: '',
          type: '',
          random: 0
        },
        form: {
          name: '',
          _id: ''
        },
        rules: {
          name: [
            {required: true, message: this.$t('validate.required'), trigger: 'blur'}
          ]
        },
        tableData: [],
        tableHead: [
          {prop: 'name', label: this.$t('configLazyLoad.robotSeven.dictionary')},
          {prop: 'createUser', label: this.$t('public.creator')},
          {prop: 'createTime', label: this.$t('public.createTime')},
          {prop: 'lastUpdater', label: this.$t('configLazyLoad.robotSeven.recentlyEdited')},
          {prop: 'lastUpdateTime', label: this.$t('configLazyLoad.robotSeven.recentlyTime')},
          {prop: 'operate', label: this.$t('monitor.operate'), special: 'operate', operate: [{name: this.$t('public.edit'), type: 'edit'}, {name: this.$t('public.delete'), type: 'delete'}]}
        ]
      }
    },
    components: {
      commonTable,
      affix
    },
    methods: {
      close () {
        this.commonDialog.isShow = false
      },
      operate (event) {
        let type = event.type
        if (type === 'delete') {
          this.deleteQuestion(event.val)
        } else if (type === 'edit') {
          this.commonDialog.isShow = true
          this.commonDialog.title = this.$t('configLazyLoad.robotSeven.editDictionary')
          this.form._id = event.val._id
          this.form.name = event.val.name
          this.commonDialog.type = 'edit'
        } else if (type === 'add') {
          this.commonDialog.isShow = true
          this.commonDialog.title = this.$t('configLazyLoad.robotSeven.addDictionary')
          this.commonDialog.type = 'add'
          this.form._id = ''
          this.form.name = ''
        }
      },
      deleteQuestion (data) {
        this.$confirm(this.$t('business.businessDeleteTip'), this.$t('configLazyLoad.robotSeven.deleteDictionary'), {
          confirmButtonText: this.$t('public.confirm'),
          cancelButtonText: this.$t('public.cancel'),
          cancelButtonClass: 'primary_btn',
          confirmButtonClass: 'primary_btn',
          type: 'warning'
        })
          .then(() => {
            this.$store.dispatch('deleteUserDictionary', {ids: data._id + '|' + data.name})
              .then(resp => {
                if (resp.success) {
                  this.search()
                  this.$message({
                    type: 'success',
                    message: this.$t(this.$t('message.deleteSucc'))
                  })
                }
              })
          })
      },
      submitForm () {
        let action = ''
        let data = {}
        if (this.commonDialog.type === 'add') {
          action = 'addUserDictionary'
          data = {_id: uuid.v1(), name: this.form.name}
        } else {
          action = 'updateUserDictionary'
          data = {dId: this.form._id, name: this.form.name}
        }
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.$store.dispatch(action, data).then((res) => {
              if (res.success) {
                this.commonDialog.isShow = false
                this.$message.success(this.$t('monitor.saveSuccess'))
                this.search(1)
              }
            })
          }
        })
      },
      search (page) {
        this.searchObj.page = typeof page === 'number' ? page : 1
        this.$store.dispatch('queryUserDictionary', this.searchObj).then((res) => {
          this.pagination.count = res.count
          this.pagination.currentPage = this.searchObj.page
          let list = res.list
          let arr = []
          for (let i = 0; i < list.length; i++) {
            let dictionary = list[i]
            let createUser = getCache('agents', dictionary.createUser)
            if (createUser) {
              createUser = createUser.displayName
            } else {
              createUser = '-'
            }
            let lastUpdater = dictionary.lastUpdater
            if (lastUpdater) {
              let agent = getCache('agents', lastUpdater)
              lastUpdater = agent ? agent.displayName : '-'
            } else {
              lastUpdater = '-'
            }
            let lastUpdateTime = dictionary.lastUpdateTime
            if (!lastUpdateTime) {
              lastUpdateTime = '-'
            }
            arr.push({operate: '1', _id: dictionary._id, name: dictionary.name, createTime: dictionary.createTime, lastUpdateTime, createUser, lastUpdater})
          }
          this.tableData = arr
        })
      }
    },
    deactivated () {
      this.commonDialog.isShow = false
    },
    beforeMount () {
      this.$store.dispatch('getCache', {type: 'agents'}).then(() => {
        this.search()
      })
    }
  }
</script>
<style lang="stylus" scoped>
  .config-margin
    padding 20px
  .search
    padding-bottom 10px
  .margin-l
    margin-left 20px
</style>
